package com.gexiaobao.pigeon.ui.adapter

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import android.widget.LinearLayout
import androidx.appcompat.widget.AppCompatImageView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import androidx.core.view.isGone
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.DataBindBaseViewHolder
import com.gexiaobao.pigeon.app.model.bean.BreedingPairTimeAxisBean
import com.gexiaobao.pigeon.app.util.DatetimeUtil
import com.gexiaobao.pigeon.databinding.ItemBreedingTrackingBinding

/**
 * @Author      : hxw
 * @Date        : 2024/9/6 15:05
 * @Describe    :
 */
class AdapterBreedingTracking(data: ArrayList<BreedingPairTimeAxisBean>) :
    BaseQuickAdapter<BreedingPairTimeAxisBean, DataBindBaseViewHolder>(R.layout.item_breeding_tracking, data) {

    private var mOnItemClickListener: OnRingItemClickListener? = null

    @SuppressLint("NotifyDataSetChanged")
    override fun convert(holder: DataBindBaseViewHolder, item: BreedingPairTimeAxisBean) {
        val binding = DataBindBaseViewHolder.getBinding(holder) as ItemBreedingTrackingBinding
        binding.data = item
        binding.executePendingBindings()//防止列表抖动

        val tvChooseDate = holder.getView<AppCompatTextView>(R.id.tvChooseDate)
        val llChooseTime = holder.getView<LinearLayout>(R.id.llChooseTime)
        val ivArrowTime = holder.getView<AppCompatImageView>(R.id.ivArrowTime)
        val llContent = holder.getView<LinearLayout>(R.id.llContent)
        val tvEggStatus = holder.getView<AppCompatTextView>(R.id.tvEggStatus)
        holder.setGone(R.id.tvRemark, item.remark.isEmpty())

        val dateLong = DatetimeUtil.convertTimeStringToTimestamp(item.date)
        tvChooseDate.text = DatetimeUtil.conversionTime(dateLong, "MM-dd")

        val isArrowGone: Boolean
        val isLayoutGone: Boolean
        val tvStatusColor: Int
        val tvTimeBg: Int
        val tvTimeColor: Int
        val ivIconTimeArrow: Drawable
        val llContentBg: Int
        val isEnable: Boolean
        val clockResource: Int
        val typeface: Typeface?

        val canEdit = item.canEdit
        val mStatusInfo = item.statusInfo
        if (!canEdit && mStatusInfo.isEmpty()) {//false-不可编辑，true-可编辑
            isLayoutGone = true
            tvTimeBg = R.drawable.round_btn_gray8_bg
            tvTimeColor = ContextCompat.getColor(context, R.color.white)
            ivIconTimeArrow = ContextCompat.getDrawable(context, R.mipmap.icon_down_arrow)!!
            tvStatusColor = ContextCompat.getColor(context, R.color.color_f65a5a)
            isArrowGone = true
            isEnable = false
            llContentBg = R.drawable.round_btn_gray8_bg
            clockResource = R.mipmap.icon_blood_breeding_clock_unselected
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        } else if (canEdit && mStatusInfo.isEmpty()) {//可编辑且为空
            isLayoutGone = false
            isArrowGone = item.statusName == context.getString(R.string.Wear_ankle_rings)
            isEnable = true
            tvTimeBg = R.drawable.round_btn_red8_bg
            tvTimeColor = ContextCompat.getColor(context, R.color.white)
            ivIconTimeArrow = ContextCompat.getDrawable(context, R.mipmap.icon_down_arrow)!!
            tvStatusColor = ContextCompat.getColor(context, R.color.color_f65a5a)
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
            llContentBg = R.drawable.round_white10_bg
            clockResource = R.mipmap.icon_blood_breeding_clock
        } else if (!canEdit && mStatusInfo.isNotEmpty()) {//不可编辑且不为空
            isLayoutGone = false
            isArrowGone = true
            isEnable = false
            tvTimeBg = R.drawable.round_white10_bg
            tvTimeColor = ContextCompat.getColor(context, R.color.black)
            ivIconTimeArrow = ContextCompat.getDrawable(context, R.mipmap.icon_arrow_down_black)!!
            tvStatusColor = ContextCompat.getColor(context, R.color.black)
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.BOLD)
            llContentBg = R.drawable.round_white10_bg
            clockResource = R.mipmap.icon_blood_breeding_clock_unselected
        } else {//可编辑 不为空
            isLayoutGone = false
            isArrowGone = item.statusName == context.getString(R.string.Wear_ankle_rings)
            isEnable = true
            tvTimeBg = R.drawable.round_btn_red8_bg
            tvTimeColor = ContextCompat.getColor(context, R.color.white)
            ivIconTimeArrow = ContextCompat.getDrawable(context, R.mipmap.icon_down_arrow)!!
            tvStatusColor = ContextCompat.getColor(context, R.color.color_f65a5a)
            llContentBg = R.drawable.round_white10_bg
            clockResource = R.mipmap.icon_blood_breeding_clock
            typeface = Typeface.create(Typeface.DEFAULT, Typeface.NORMAL)
        }
        holder.setGone(R.id.llOneEgg, isLayoutGone)
        holder.setGone(R.id.ivArrowDown, isArrowGone)
        tvEggStatus.setTextColor(tvStatusColor)
        llChooseTime.background = ContextCompat.getDrawable(context, tvTimeBg)
        holder.setTextColor(R.id.tvChooseDate, tvTimeColor)
        ivArrowTime.background = ivIconTimeArrow
        llContent.background = ContextCompat.getDrawable(context, llContentBg)
        holder.setEnabled(R.id.llChooseTime, isEnable)
        tvEggStatus.isEnabled = isEnable
        holder.setBackgroundResource(R.id.ivTrackTimeClock, clockResource)
        tvEggStatus.typeface = typeface

        val sInfoStatus = item.statusInfo.ifEmpty { context.getString(R.string.activity_market_product_sku_dialog_please_select) }
        tvEggStatus.text = sInfoStatus

        tvEggStatus.isGone = item.statusName == context.getString(R.string.Wear_ankle_rings)

        holder.itemView.apply {
            binding.recyclerView.adapter = null
            if (item.ringList.isEmpty()) {
                holder.setGone(R.id.recyclerView, true)
                return
            }
            holder.setGone(R.id.recyclerView, false)
            val mAdapterRing = AdapterRingStr(item.ringList)
            binding.recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapterRing
                mAdapterRing.notifyDataSetChanged()
            }

            mAdapterRing.setOnItemClickListener { _, _, position ->
                val ringNo = mAdapterRing.data[position].ringId
                mOnItemClickListener?.onRingItemClick(holder.layoutPosition, position, mAdapterRing.data[position].id, ringNo)
            }
        }
    }

    fun setOnRingClickListener(listener: OnRingItemClickListener?) {
        mOnItemClickListener = listener
    }

    interface OnRingItemClickListener {
        fun onRingItemClick(positionP: Int, position: Int, id: Int, ringNumber: String)
    }
}